#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 带附件的添加 chenhua
import smtplib
import time
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import schedule
import random

ISOTIMEFORMAT = ''


# 定义执行任务
def job():
    sentemail()


# 支持的各种任务格式
# 设置调度的参数，这里是每10秒执行一次。
schedule.every(10).seconds.do(job)
# 每小时执行
# schedule.every().hour.do(job)
# 每天12:25执行
# schedule.every().day.at("12:25").do(job)
# 每2到5分钟时执行
#schedule.every(1).to(10).minutes.do(job)
# 每星期4的19:15执行
# schedule.every().thursday.at("19:15").do(job)
# 每第17分钟时就执行
# schedule.every().minute.at(":17").do(job)
# 定时任务的设置每20分钟。
#schedule.every(20).minutes.do(job)


def sentemail():
    email_header_list = [

        "来自疾病预警系统的贴心提示"
    ]
    email_header = random.choice(email_header_list)
    sentdate = str(time.strftime(ISOTIMEFORMAT, time.localtime()))
    # 设置发送服务器地址
    host = 'smtp.163.com'
    # 设置发件服务器端口，注意这里有SSL和非SSL两种形式。一遍都是ssl
    port = 465
    # 请填写自己的163邮箱
    sender_account = '18360939363@163.com'
    # 请填写自己的邮箱密码
    sender_password = '211416038A'
    # 请填写接收的邮箱
    # receiver_account = '1287572725@qq.com'
    receiver_account = '1127698936@qq.com'
    # 构建邮件主体内容
    message = MIMEMultipart()
    message['subject'] = sentdate + "疾病预警监控系统：" + email_header
    message['from'] = sender_account
    message['to'] = receiver_account
    # 构建邮件正文内容
    body = '<h1>' + sentdate + '</h1><h2>' + "疾病预警监控系统：" + email_header #+ '</h2><p>陈华</p>'
    message_body = MIMEText(body, 'html')
    message.attach(message_body)
    # # 添加附件1
    # filename = 'C:/Users/Administrator/Desktop/改进.png'
    # message_accessory = MIMEText(open(filename, 'rb').read(), 'base64', 'UTF-8')
    # message_accessory["Content-type"] = 'application/octet-stream'
    # message_accessory["Content-Disposition"] = 'attachment; filename=' + filename + '"'
    # message.attach(message_accessory)
    # # 添加附件2
    # filename2 = 'C:/Users/Administrator/Desktop/改进.png'
    # message_accessory2 = MIMEText(open(filename2, 'rb').read(), 'base64', 'UTF-8')
    # message_accessory2["Content-type"] = 'application/octet-stream'
    # message_accessory2["Content-Disposition"] = 'attachment; filename=' + filename2 + '"'
    # message.attach(message_accessory2)
    try:
        sender = smtplib.SMTP_SSL(host, port)
        sender.login(sender_account, sender_password)
        sender.sendmail(sender_account, receiver_account, message.as_string())
        print('Send Email Done')
    except Exception as e:
        print(e)


if __name__ == '__main__':
    while True:
        schedule.run_pending()
